﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Text;
using MediSIIM.Message;
using MediSIIM.Compression;
using System.Xml.Serialization;
using System.Xml;
using ZMQ;
using System.Runtime.InteropServices;

namespace SIIMWeb.YIBAO
{
    public class YIBAOJK
    {

        static ZMQ.Context mqContext = new Context(1);
        /// <summary>
        /// 获得消息头:[GUID、时间、消息类型、版本号、内容长度、内容]
        /// </summary>
        /// <returns></returns>
        public static string GetXmlHead()
        {
            return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SIIMMessage>" +
                "  <Head>" +
                "    <MessageId>{{{0}}}</MessageId>" +
                "    <MessageDate>{1:yyyy-MM-dd HH:mm:ss}</MessageDate>" +
                "    <MessageType>{2}</MessageType>\r\n" +
                "    <Version>{3}</Version>" +
                "    <Sender>HIS</Sender>" +
                "    <Receiver>SIIM</Receiver>" +
                "  </Head>" +
                "  <Body>" + "    <Length>{4}</Length>" +
                "    <Content>{5}</Content>" +
                "  </Body>" +
                "</SIIMMessage>";
        }
        static Socket socket;
        public static string ZMQCall(string ylbxid, string jiaoyimc, string siimurl, string xcontent)
        {
            if (socket == null)
            {
                socket = mqContext.Socket(SocketType.DEALER);
                socket.Connect(siimurl);
            }
            else
            {
                if (socket.Address != siimurl)
                {
                    socket.Connect(siimurl);
                }
            }
            string msgType = ylbxid + "." + jiaoyimc;
            string compress = string.Empty;
            BZip2.Compress(xcontent, out compress);
            var message = string.Format(GetXmlHead(),
                Guid.NewGuid().ToString(), DateTime.Now, ylbxid + "." + jiaoyimc, ConfigurationManager.AppSettings["Version"], Encoding.UTF8.GetByteCount(xcontent), compress);

            var sendstat = socket.Send("", Encoding.UTF8, SendRecvOpt.SNDMORE);
            if (sendstat == SendStatus.Sent)
            {
                sendstat = socket.Send(msgType, Encoding.UTF8, SendRecvOpt.SNDMORE);
                sendstat = socket.Send(message, Encoding.UTF8);
                var list = new List<byte>();

                string msg = "";
                foreach (var item in socket.RecvAll(Encoding.UTF8))
                {
                    msg = item;
                }
                //msg = Encoding.UTF8.GetString(list.ToArray());
                if (msg == "")
                {
                    throw new System.Exception("连接医保中间件失败");
                }
                var siimmsg = MessageParser.Parse(msg, ref msg);
                if (siimmsg == null)
                {

                    return ("<?xml version='1.0' encoding='utf-8' ?><result>" +
                        msg +
                    "</result>").Replace("'", "\"");
                }
                return siimmsg.Body.Content;
            }
            else
            {
                throw new System.Exception("连接医保中间件失败");
            }
        }
    }
}